package com.sc.admin.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.sc.admin.common.ResponseResult;
import com.sc.admin.pojo.Goods;
import com.sc.admin.pojo.GoodsCat;
import com.sc.admin.pojo.Menu;
import com.sc.admin.service.GoodsCatService;
import com.sc.admin.service.GoodsService;
import com.sc.admin.utils.EasyuiTreeNode;


@Controller
@RequestMapping("/goods")
public class GoodsController {
	@Autowired
	private GoodsService goodsService;
	
	@Autowired
	private GoodsCatService goodsCatService;
	
	@RequestMapping("/page")
	public String goodsPage() {
		return "goods-list";
	}
	
	@RequestMapping("/list")
	public @ResponseBody Object goodsList(int page,int rows) {
		/*int count = goodsService.getCount();
		List<Goods> goodss = goodsService.getGoodss(page, rows);
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("total", count);
		map.put("rows", goodss);
		return map;*/
		return null;
	}
	
	@RequestMapping(value="/add",method=RequestMethod.GET)
	public String addPage() {
		return "goods-add";
	}
	
	@RequestMapping(value="/cat/tree",method=RequestMethod.POST)
	public @ResponseBody Object catsTree(@RequestParam(defaultValue="0")int id) {
		List<GoodsCat> cats = goodsCatService.getGoodsCatByPid(id);
		List<EasyuiTreeNode> nodes = new ArrayList<EasyuiTreeNode>();
		for(GoodsCat cat : cats) {
			EasyuiTreeNode node = catToNode(cat);
			nodes.add(node);
		}
		return nodes;
	}
	
	
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public @ResponseBody Object add(Goods goods) {
		goodsService.add(goods);
		return new ResponseResult().ok();
	}
	
	@RequestMapping(value="/name/check",method=RequestMethod.GET)
	public @ResponseBody Object check(String name) {
		/*Goods goods = goodsService.checkName(name);
		return new ResponseResult().ok(goods);*/
		return null;
	}
	
	@RequestMapping(value="/edit",method=RequestMethod.POST)
	public @ResponseBody Object edit(Goods goods) {
		/*goodsService.update(goods);
		return new ResponseResult().ok();*/
		return null;
	}
	
	private EasyuiTreeNode catToNode(GoodsCat cat) {
		EasyuiTreeNode node = new EasyuiTreeNode();
		node.setId(cat.getId());
		node.setText(cat.getName());
		node.setState(cat.getIsParent()?"closed":"open");
		return node;
	}
}
